﻿using Business;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BVVD_Website.user_zone
{
    public partial class doiMatkhau : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnCHANGE_Click(object sender, EventArgs e)
        {
            if (Session["user"] != null & Page.IsValid)
            {
                LST_NSD curUser = Session["user"] as LST_NSD;
                BVVDLibrary bl = new BVVDLibrary();
                BVVDDataContext db = new BVVDDataContext();
                var user = (from u in db.LST_NSDs
                            where u.MANSD.Equals(curUser.MANSD)
                            select u).SingleOrDefault();

                F_DOIMATKHAU f = new F_DOIMATKHAU
                {
                    MANSD = curUser.MANSD,
                    MATKHAUCU = curUser.MATKHAU,
                    NGAYDOI = DateTime.Now,
                    MATKHAUMOI = bl.encryptPassword(txtNEWPASS1.Text),
                    LANDOI = getChangeNumber(curUser.MANSD) + 1

                };
                db.F_DOIMATKHAUs.InsertOnSubmit(f);
                user.MATKHAU = bl.encryptPassword(txtNEWPASS1.Text);
                db.SubmitChanges();
                rnAlert.Text = "Thay đổi mật khẩu thành công";
                rnAlert.Show();                          
                             
                
            }
            else
            {
               

            }

        }

        private int getChangeNumber(string nsd)
        {
            BVVDDataContext db = new BVVDDataContext();
            var number = (from num in db.F_DOIMATKHAUs
                          where num.MANSD.Equals(nsd)
                          select num.LANDOI).ToList();
            
                return number.Max().GetValueOrDefault(0);
           


        }


    }
}